﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VolunteerManager
{
    

    /// <summary>
    /// CheckboxGroup.xaml 的交互逻辑
    /// </summary>
    public partial class CheckboxGroup : UserControl
    {
        public CheckboxGroup()
        {
            InitializeComponent();
            this._values = new List<string>();


        }

        void cb_Unchecked(object sender, RoutedEventArgs e)
        {
            _values.Remove((sender as CheckBox).Content.ToString());

            CheckedValues = toS();
        }


        void cb_Checked(object sender, RoutedEventArgs e)
        {
            _values.Add((sender as CheckBox).Content.ToString());
            CheckedValues = toS();
        }

        string toS()
        {
            string v = "";
            foreach (string s in _values)
            {
                if (v == "")
                {
                    v += s;
                }
                else
                {
                    v += "、" + s;
                }
            }
            return v;
        }
        private List<string> _values { get; set; }

        public string CheckedValues
        {
            get { return GetValue(CheckedValuesProperty).ToString(); }
            set { SetValue(CheckedValuesProperty, value); }
        }

        private bool flag = false;
        private void _checkBoxs(object o)
        {
            string v = "";
            if (o != null) v = o.ToString();

            string[] vs = v.Split('、');
            _values = vs.ToList<string>();
            bool hasCheck = false;
            foreach (UIElement element in this.panel.Children)
            {
                if (element is CheckBox)
                {
                    CheckBox cb = element as CheckBox;
                    cb.Checked -= cb_Checked;
                    cb.Unchecked -= cb_Unchecked;
                    cb.IsChecked = false;
                    string tag = cb.Content.ToString();
                    if (vs.Contains(tag))
                    {
                        hasCheck = true;
                        cb.IsChecked = true;
                    }
                    cb.Checked += cb_Checked;
                    cb.Unchecked += cb_Unchecked;

                }
            }
            
            if (hasCheck == false && v != "" && v != "其它") {
                OtherText = v;
            }
        }


        private List<string> b0 = new List<string>() { "农/林/牧/水利业员", "医疗/卫生/体育", "教育/文化/广播", "金融/保险", "国家机关/社会团体", "地质/斟探", "工业", "交通/邮电", "建筑", "商业" };
        private List<string> b1 = new List<string>() { "公务员", "教师", "学生", "军人", "工人", "农民", "医务人员", "科技人员", "企事业单位职员",
        "家庭主妇", "离/退休人员"};
        private List<string> b2 = new List<string>() { "老人陪护", "照顾小孩", "心理拓展", "助教", "卫生清扫", "服从安排"};


        private void CreateBoxs()
        {
            List<string> list = new List<string>();
            if (Type == 1)
            {
                list = b0;
            }
            else if (Type == 2)
            {
                list = b1;
            }
            else if (Type == 3)
            {
                list = b2;
            }

            foreach (string s in list)
            {
                this.panel.Children.Insert(panel.Children.Count-2, new CheckBox() { Content = s });
            }
        }

        public int Type
        {
            get
            {
                return int.Parse(GetValue(TypeProperty).ToString());
            }
            set
            {
                SetValue(TypeProperty, value);
            }
        }
        /// <summary>
        /// 显示内容
        /// </summary>
        public static DependencyProperty TypeProperty =
            DependencyProperty.Register("Type", typeof(int), typeof(CheckboxGroup), new PropertyMetadata(0, new PropertyChangedCallback(TypePropertyCangedCallback)));

        private static void TypePropertyCangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs arg)
        {
            if (sender != null && sender is CheckboxGroup)
            {
                CheckboxGroup clock = sender as CheckboxGroup;
                clock.CreateBoxs();
            }
        }

        /// <summary>
        /// 选中项目的值，以字符串表示
        /// </summary>
        public static readonly DependencyProperty CheckedValuesProperty =
           DependencyProperty.Register("CheckedValues", typeof(string), typeof(CheckboxGroup), 
                    new PropertyMetadata(null, new PropertyChangedCallback(TimePropertyChangedCallback)));

        //更新界面复选框内容
        private static void TimePropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs arg)
        {
            if (sender != null && sender is CheckboxGroup)
            {
                CheckboxGroup clock = sender as CheckboxGroup;
                clock._checkBoxs(arg.NewValue);
            }
        }


        public string OtherText
        {
            get { return GetValue(OtherTextProperty).ToString(); }
            set { SetValue(OtherTextProperty, value); }
        }

        /// <summary>
        /// 其它的内容
        /// </summary>
        public static readonly DependencyProperty OtherTextProperty =
           DependencyProperty.Register("OtherText", typeof(string), typeof(CheckboxGroup), new PropertyMetadata("")); 




        public string Label
        {
            get { return GetValue(LabelProperty).ToString(); }
            set { SetValue(LabelProperty, value); }
        }

        /// <summary>
        /// 标题
        /// </summary>
        public static readonly DependencyProperty LabelProperty =
           DependencyProperty.Register("Label", typeof(string), typeof(CheckboxGroup), new PropertyMetadata("2013"));



    }
}
